package com.kdtech.analyse.video;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class Ku6Analyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://v.ku6.com/show/2orw2tNz6lBF39LS_E-_3g...html
		//http://v.ku6.com/special/show_6309035/znKawgjFfeertrtsPQCbIg...html
		String[] regex = {
				"http://v.ku6.com/show/.*...html.*"
				,"http://v.ku6.com/special/show_[0-9]*/.*...html.*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = 0l;



		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题
		 */
		Elements select2 = doc.select("p.title");
		if(select2.size()>0){
			title = select2.get(0).ownText().replace(">", "");
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("h1 span").text();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("title").text().replace("在线观看 - 酷6视频", "");
		}


		/*
		 * 解析时间
		 */
		String text = doc.select("div.infoBox>dl.infoList>dd>em").text();
		date = DateUtils.matchDate(text);
		if (date==null){
			date  = JSoupUtils.matchDate(doc,"发布时间");
		}

		/*
		 * 解析内容
		 */
		Elements select = doc.select("dl.infoList>dd");
		if(select.size()>1){
			desc= select.get(1).select("span").text();
		}
		if (StringUtils.isBlank(desc)){
			desc = doc.select(".ckl_xaxi").text();
		}
		if(StringUtils.isBlank(title)){
		}
		video.setTitle(title);
		video.setDate(date);
		video.setContent(desc);

		String author = doc.select(":containsOwn(上 传 者：) a").text();
		if (StringUtils.isBlank(author)){
			 author = doc.select(":containsOwn(上传者) a").text();
		}

		if (StringUtils.isBlank(author)){
			Elements select3 = doc.select("div.vtil_main div.vt_user div.vt_btn_div a.kw_btn");
			if(select3!=null && select3.size()!=0){
				author=select3.get(0).text();
				author=author.replace("上传者：", "");
			}
		}
		video.setAuthor(author);
		/**
		 * 解析播放次数URL
		 * http://v6.stat.ku6.com/dostatv.do?method=getVideoPlayCount&v=BFWiGOQLaCyRNZ1SQo85Ew..
		 */
		int pos = html.indexOf("snyu_page_params='vid=");
		if(pos!=-1){
			String tmp=html.substring(pos+"snyu_page_params='vid=".length());
			pos = tmp.indexOf("&category=");
			if(pos!=-1){
				tmp = tmp.substring(0,pos);
				video.setUpdateUrl("http://v6.stat.ku6.com/dostatv.do?method=getVideoPlayCount&v="+tmp);
				 NewsMeta update = Update(video);
				 if(update!=null){
					 video.setClickNum(update.getClickNum());
				 }
			}
		}
		return video;
	}
	
	public NewsMeta Update(NewsMeta meta) {

		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						int pos = html.lastIndexOf("count:\"");
						if(pos!=-1){
							html = html.substring(pos+"count:\"".length());
							pos = html.indexOf("\"");
							if(pos!=-1){
								html=html.substring(0,pos);
								try {
									int parseInt = Integer.parseInt(html);
									meta.setClickNum(parseInt);
									return meta;
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
								}
							}
						}
					}
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		Ku6Analyse a = new Ku6Analyse();
		String url = "http://v.ku6.com/special/show_6591266/Tl6do9vaF1EEX0k2mtYXEQ...html?hpsrc=1_22_1_1_0";
		if(a.isDetailPage(url)){
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}else{
			System.out.println("不匹配规则");
		}

	}





	
}
